package wpRelations;

import java.io.PrintWriter;
import java.util.ArrayList;

import processing.core.PApplet;

public class TsvWriter extends PApplet {
	PrintWriter output;
	String title="Coby_Miller";
	boolean isTitleAPerson;
	public static final int ARTICLE = 0;
	public static final int PERSON = 1;
	public static final int NON_PERSON = 2;
	public static final int CATEGORY = 3;
	
	public void setup() {
		  output = createWriter(title + "_links.tsv");
		  wArticle article_zero = new wArticle(this, title);
		  isTitleAPerson = article_zero.checkPerson();
		  ArrayList<wArticle> links = article_zero.getLinks();
		  //// writes all links from article_zero to file
		  for (int i = 0; i < links.size(); i++) {
			System.out.println("writing link " + i + " out of " + links.size());
			String in = article_zero.title;
			String out = links.get(i).title;
			String inType;
			if (isTitleAPerson) {
				inType = "person";
			} else {
				inType = "non_person";
			}
			String outType;
			boolean isAPerson = links.get(i).checkPerson();
			if (isAPerson) {
				outType = "person";
			} else {
				outType = "non_person";
			}
			output.println(in + "\t" + out + "\t" + inType + "\t" + outType);
		  }
		  
		  ArrayList<wArticle> backLinks = article_zero.getBackLinks();
		  //// writes all backlinks to article_zero to file
		  for (int i = 0; i < backLinks.size(); i++) {
			System.out.println("writing backLink " + i + " out of " + backLinks.size());
			String in =backLinks.get(i).title; 
			String out = article_zero.title;
			String inType;
			boolean isAPerson = backLinks.get(i).checkPerson();
			if (isAPerson) {
				inType = "person";
			} else {
				inType = "non_person";
			}
			String outType;
			if (isTitleAPerson) {
				outType = "person";
			} else {
				outType = "non_person";
			}

			output.println(in + "\t" + out + "\t" + inType + "\t" + outType);
		  }

//dauert leider zu lange!-D
//		  //// writes all links from all links from article_zero to file
//		  ArrayList<wArticle> linksFromLinks;
//		  wArticle link;
//		  for (int i = 0; i < links.size(); i++) {
//			link = links.get(i);
//			linksFromLinks = link.getLinks();
//			for (int j = 0; j < linksFromLinks.size(); j++) {
//				System.out.println("writing toLink " + j + " out of " + linksFromLinks.size() + " | fromLink " + i + " out of " + links.size());
//				String in = links.get(i).title;
//				String out = linksFromLinks.get(j).title;
//				String inType;
//				boolean isAPerson = links.get(i).checkPerson();
//				if (isAPerson) {
//					inType = "person";
//				} else {
//					inType = "non_person";
//				}
//				String outType;
//				isAPerson = linksFromLinks.get(j).checkPerson();
//				if (isAPerson) {
//					outType = "person";
//				} else {
//					outType = "non_person";
//				}
//				output.println(in + "\t" + out + "\t" + inType + "\t" + outType);
//				
//			}
//			  }
		
		  ArrayList<wCategory> categories = article_zero.getCategories();
		  //// writes all categories containing article_zero to file
		  for (int i = 0; i < categories.size(); i++) {
			System.out.println("writing category " + i + " out of " + categories.size());
			String in =categories.get(i).title; 
			String out = article_zero.title;
			String inType = "category";
			String outType;
			if (isTitleAPerson) {
				outType = "person";
			} else {
				outType = "non_person";
			}

			output.println(in + "\t" + out + "\t" + inType + "\t" + outType);
		  }
		  
//dauert leider zu lange!-D
//		  //// writes all category contents from categories containing article_zero to file
//		  for (int i = 0; i < categories.size(); i++) {
//			categories.get(i).setSubCatsPages();
//			for (int j = 0; j < categories.get(i).subCats.size(); j++) {
//				System.out.println("writing subCategory " + j + " out of " + categories.get(i).subCats.size());
//				String in =categories.get(i).title; 
//				String out = categories.get(i).subCats.get(j).title;
//				String inType = "category";
//				String outType = "category";
//				output.println(in + "\t" + out + "\t" + inType + "\t" + outType);
//			}
//		
//			  for (int j = 0; j < categories.get(i).getSubPages().size(); j++) {
//					System.out.println("writing page " + j + " out of " + categories.get(i).pages.size());
//					String in =categories.get(i).title; 
//					String out = categories.get(i).pages.get(j).title;
//					String inType = "category";
//					String outType;
//					boolean isAPerson = categories.get(i).pages.get(j).checkPerson();
//					if (isAPerson) {
//						outType = "person";
//					} else {
//						outType = "non_person";
//					}
//					output.println(in + "\t" + out + "\t" + inType + "\t" + outType);
//				  }
//
//		  }
		  


		  
		  
		    output.flush(); // Write the remaining data
		    output.close(); // Finish the file
		    System.out.println("finished.");
		    super.exit(); // Stop the program

	}
	
}
